﻿#region Usings
using System.Collections.Generic;

using BK.BlusinessLogic.Exceptions;
using BK.Model;
using BK.Repositories.Abstract;
#endregion

namespace BK.BlusinessLogic
{
    public class CurrenciesBl : IGeneralBl<Currency>
    {
        #region Readonly & Static Fields
        private readonly ICurrenciesRepository _currenciesRepository;
        #endregion

        #region C'tors
        public CurrenciesBl(ICurrenciesRepository currenciesRepository)
        {
            _currenciesRepository = currenciesRepository;
        }
        #endregion

        #region Instance Methods
        public void Add(int userId, Currency currency)
        {
            if (_currenciesRepository.GetBy(userId, currency.Name) == null)
            {
                _currenciesRepository.Save(currency);
            }
            else
            {
                throw new UniqueException();
            }
        }

        public Currency GetBy(int userId, string currencyName)
        {
            return _currenciesRepository.GetBy(userId, currencyName);
        }
        #endregion

        #region IGeneralBl<Currency> Members
        public IEnumerable<Currency> GetByUserId(int userId)
        {
            return _currenciesRepository.GetByUserId(userId);
        }

        public Currency GetById(int id)
        {
            return _currenciesRepository.GetById(id);
        }

        public void Delete(int id)
        {
            _currenciesRepository.Delete((id));
        }

        public void Save(Currency currency)
        {
            _currenciesRepository.Save(currency);
        }
        #endregion
    }
}